﻿function loadXMLDoc(url) {
    var xmlhttp;
    var txt="", x, xx, i, a,b;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            txt = "<table border='1'  width='100%' align='center'><tr class='table'><th>Tên Sách</th><th>Chuyên mục</th><th>Tác giả</th><th>Tóm tắt nội dung</th></tr>";
            x = xmlhttp.responseXML.documentElement.getElementsByTagName("book");
            for (i = 0; i < x.length; i++) {
                txt = txt + "<tr>";
                b = x[i].getElementsByTagName("id")[0].firstChild.nodeValue;           

                xx = x[i].getElementsByTagName("title");
                {
                    try {
                        txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                    }
                    catch (er) {
                        txt = txt + "<td> </td>";
                    }
                }
                xx = x[i].attributes;
                try {
                    txt = txt + "<td>" + xx.getNamedItem("category").value + "</td>";
                }
                catch (er) {
                    txt = txt + "<td> </td>";
                }
                xx = x[i].getElementsByTagName("author");
                {
                    try {
                        txt = txt + "<td width='15%'>";
                        for (a = 0; a < xx.length; a++) {
                            txt = txt + xx[a].firstChild.nodeValue;
                            txt = txt + "<br>";
                        }
                        txt = txt + "</td>";
                    }
                    catch (er) {
                        txt = txt + "<td> </td>";
                    }
                }
                xx = x[i].getElementsByTagName("Description");

                {
                    try {
                        txt = txt + "<td align='center' width='50%'>"

                            + "<div id='masterdiv'>"
                                 + "<div id='" + b + 1 + "' class='kieu' style='cursor:hand;' onclick='SwitchMenu(" + b + ");'>"
                                     + "<img id='" + b+ 1 + 1 + "'  src='images/down_16.png'>"
                                    + "<a >" + "Ẩn / Hiển thị" + "</a>"
                                + "</div>"
                                + "<span style='display: none;' class='options' id='" + b + "'>"
                                      + "<div class='Options'>" + xx[0].firstChild.nodeValue + "</div>"
                                  + "</span>"
                            + "</div>"
                            + "</td>";
                    }
                    catch (er) {
                        txt = txt + "<td> </td>";
                    }
                }
            }
            txt = txt + "</tr>";

        }
        txt = txt + "</table>";
        document.getElementById('books').innerHTML = txt;
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}


function SwitchMenu(id) {
    var xmlhttp;
    var txt="", x, xx, b;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            x = xmlhttp.responseXML.documentElement.getElementsByTagName("book");
            for (var a = 0; a < x.length; a++) {
                if (id == x[a].getElementsByTagName("id")[0].firstChild.nodeValue) {
                    xx = x[a].getElementsByTagName("Description");
                    {
                        try {
                            txt = xx[0].firstChild.nodeValue;
                        }
                        catch (er) {

                        }
                    }
                }

            }
        }


        if (document.getElementById) {
            var el = document.getElementById(id);
            var el1 = document.getElementById(id + '1');
            var ima = document.getElementById(id + '1' + '1');
            var tam = document.getElementById(id + '1').innerHTML;
            var ar = document.getElementById("masterdiv").getElementsByTagName("span");
            el.innerHTML = txt;   
            if (el.style.display != "block") {
                for (var i = 0; i < ar.length; i++) {
                    if (ar[i].className == "options")
                        ar[i].style.display = "none";
                }
                el.style.display = "block";
                ima.src = "images/up_16.png";
                
            } else {
                el.style.display = "none";
                ima.src = "images/down_16.png";
               
            }
        }
    }
    xmlhttp.open("GET", 'books.xml', true);
    xmlhttp.send();
}